package com.happyscan138.makefun;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.happyscan138.makefun.Fragment.battle_Fragment;
import com.happyscan138.makefun.Fragment.main_Fragment;
import com.happyscan138.makefun.Fragment.more_Fragment;
import com.happyscan138.makefun.Fragment.news_Fragment;
import com.happyscan138.makefun.base.PollingUtils;
import com.happyscan138.makefun.service.PollingService;

import static com.happyscan138.makefun.R.id.plus_im;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private boolean clicked=false;//记住加号点击状态
    private RadioButton burronMain,buttonBattle,buttonNews,buttonMore;
    private RadioGroup radioGroup;
    private Animation rotate,rotateReturn;
    private ImageView iamge_plus;
    private main_Fragment mainFragment;
    private battle_Fragment battleFragment;
    private news_Fragment newsFragment;
    private more_Fragment moreFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private ImageButton creatQuestion,creatBattle;
    private FrameLayout frameLayout;
    private PopupWindow popuWindow1;
    private View contentView1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("onstart","onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        PollingUtils.startPollingService(this, 5, PollingService.class, PollingService.ACTION);
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        mainFragment= new main_Fragment();
        transaction.replace(R.id.fragment_container, mainFragment);
        transaction.commit();

    }

    public void initView(){
        creatBattle=(ImageButton)findViewById(R.id.creat_battle);

        burronMain=(RadioButton) findViewById(R.id.main_main);
        buttonBattle=(RadioButton) findViewById(R.id.radiobattle);
        buttonNews=(RadioButton) findViewById(R.id.radioNews);
        buttonMore=(RadioButton) findViewById(R.id.buttonMore);
        radioGroup=(RadioGroup)findViewById(R.id.radiogroup);
        iamge_plus=(ImageView) findViewById(R.id.plus_im) ;
        frameLayout=(FrameLayout)findViewById(R.id.fragment_container);
        burronMain.setOnClickListener(this);
        buttonBattle.setOnClickListener(this);
        buttonNews.setOnClickListener(this);
        buttonMore.setOnClickListener(this);
        iamge_plus.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.main_main:
                transaction = fragmentManager.beginTransaction();
                mainFragment= new main_Fragment();
                transaction.replace(R.id.fragment_container, mainFragment);
                transaction.commit();
                break;

            case R.id.radiobattle:
                transaction = fragmentManager.beginTransaction();
                battleFragment= new battle_Fragment();
                transaction.replace(R.id.fragment_container, battleFragment);
                transaction.commit();
                break;
            case R.id.radioNews:
                transaction = fragmentManager.beginTransaction();
                newsFragment= new news_Fragment();
                transaction.replace(R.id.fragment_container, newsFragment);
                transaction.commit();
                break;
            case plus_im: {
                rotate = AnimationUtils.loadAnimation(MainActivity.this, R.anim.new_creat);
                rotateReturn=AnimationUtils.loadAnimation(MainActivity.this,R.anim.new_creat_return);
                clicked = !clicked;
                //执行动画
                iamge_plus.startAnimation(rotate);
                if (popuWindow1 == null) {
                    LayoutInflater mLayoutInflater = LayoutInflater.from(this);
                    contentView1 = mLayoutInflater.inflate(R.layout.center_more_window, null);
                    creatQuestion=(ImageButton) contentView1.findViewById(R.id.creat_question);
                    creatBattle=(ImageButton) contentView1.findViewById(R.id.creat_battle);
                    creatBattle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivity(new Intent(MainActivity.this,creatBattle.class));
                        }
                    });
                    creatQuestion.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivity(new Intent(MainActivity.this,creatQuestion.class));
                        }
                    });
                    popuWindow1 = new PopupWindow(contentView1, ViewPager.LayoutParams.MATCH_PARENT,
                            ViewPager.LayoutParams.WRAP_CONTENT);
                }

                ColorDrawable cd = new ColorDrawable(0x000000);
                popuWindow1.setBackgroundDrawable(cd);
                //产生背景变暗效果
                WindowManager.LayoutParams lp=getWindow().getAttributes();
                lp.alpha = 0.4f;
                getWindow().setAttributes(lp);
                popuWindow1.setOutsideTouchable(true);
                popuWindow1.setTouchable(true);
                popuWindow1.setFocusable(true);
                popuWindow1.update();
                popuWindow1.setBackgroundDrawable(new BitmapDrawable());
                popuWindow1.showAtLocation(frameLayout, Gravity.NO_GRAVITY,0, 920);
                popuWindow1.setOnDismissListener(new PopupWindow.OnDismissListener(){
                    //在dismiss中恢复透明度
                    public void onDismiss(){
                        WindowManager.LayoutParams lp=getWindow().getAttributes();
                        lp.alpha = 1f;
                        iamge_plus.startAnimation(rotateReturn);
                        getWindow().setAttributes(lp);
                    }
                });
                break;
            }
            case R.id.buttonMore:
                transaction = fragmentManager.beginTransaction();
                moreFragment= new more_Fragment();
                transaction.replace(R.id.fragment_container, moreFragment);
                transaction.commit();
                break;

        }
    }


    @Override
    protected void onStart() {
        Log.e("onstart","onstart");

        super.onStart();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Stop polling service
        PollingUtils.stopPollingService(this, PollingService.class, PollingService.ACTION);
    }

}
